﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using LFSRecord2.Model;
using System.Diagnostics;

namespace LFSRecord2.View.TimelineStructures
{
    /// <summary>
    /// Interaction logic for AudioRegionControl.xaml
    /// </summary>
    public partial class AudioRegionControl : Grid
    {
        public static readonly DependencyProperty StartTimeProperty = DependencyProperty.Register(
            "StartTime",
            typeof(double),
            typeof(AudioRegionControl),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnStartTimeChanged))
        );
        public double StartTime
        {
            get { return (double)GetValue(StartTimeProperty); }
            set { SetValue(StartTimeProperty, value); }
        }
        static void OnStartTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AudioRegionControl arc = (AudioRegionControl)d;
            arc.Width = (arc.EndTime - arc.StartTime) * LfsRecordController.project.FPS;
        }

        public static readonly DependencyProperty EndTimeProperty = DependencyProperty.Register(
            "EndTime",
            typeof(double),
            typeof(AudioRegionControl),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnEndTimeChanged))
        );
        public double EndTime
        {
            get { return (double)GetValue(EndTimeProperty); }
            set { SetValue(EndTimeProperty, value); }
        }
        static void OnEndTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AudioRegionControl arc = (AudioRegionControl)d;
            arc.Width = (arc.EndTime - arc.StartTime) * LfsRecordController.project.FPS;
        }

        public static readonly DependencyProperty AudioFilePathProperty = DependencyProperty.Register(
            "AudioFilePath",
            typeof(string),
            typeof(AudioRegionControl),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnAudioFilePathChanged))
        );
        public string AudioFilePath
        {
            get { return (string)GetValue(AudioFilePathProperty); }
            set { SetValue(AudioFilePathProperty, value); }
        }
        static void OnAudioFilePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AudioRegionControl arc = (AudioRegionControl)d;
            arc.writeNameLabel();
        }

        public static readonly DependencyProperty IsCollapsedProperty = DependencyProperty.Register(
            "IsCollapsed",
            typeof(bool),
            typeof(AudioRegionControl),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsCollapsedChanged))
        );
        public bool IsCollapsed
        {
            get { return (bool)GetValue(IsCollapsedProperty); }
            set { SetValue(IsCollapsedProperty, value); }
        }
        static void OnIsCollapsedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AudioRegionControl arc = (AudioRegionControl)d;
            arc.drawWaveView();
        }

        public AudioRegionControl()
        {
            InitializeComponent();
            drawWaveView();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            AudioRegion ar = (AudioRegion)DataContext;

            Binding myBinding = new Binding("StartTime");
            myBinding.Mode = BindingMode.OneWay;
            myBinding.Source = ar;
            SetBinding(AudioRegionControl.StartTimeProperty, myBinding);

            myBinding = new Binding("EndTime");
            myBinding.Mode = BindingMode.OneWay;
            myBinding.Source = ar;
            SetBinding(AudioRegionControl.EndTimeProperty, myBinding);

            myBinding = new Binding("AudioFilePath");
            myBinding.Mode = BindingMode.OneWay;
            myBinding.Source = ar;
            SetBinding(AudioRegionControl.AudioFilePathProperty, myBinding);

            myBinding = new Binding("WaveView");
            myBinding.Mode = BindingMode.OneWay;
            myBinding.Source = ar.AudioStream;
            WaveViewPath.SetBinding(Image.SourceProperty, myBinding);
        }

        private void writeNameLabel()
        {
            AudioName.Text = Path.GetFileNameWithoutExtension(AudioFilePath);
        }

        private void drawWaveView()
        {
            WaveViewPath.Visibility = IsCollapsed ? Visibility.Collapsed : Visibility.Visible;
            if (IsCollapsed)
            {
                // Hide waveview
                Debug.WriteLine("Hiding wave view");
                return;
            }

            // Draw waveview
            Debug.WriteLine("Drawing wave view");
        }
    }
}
